package com.sharemarking.application.common.controller;

import java.awt.image.BufferedImage;
import java.io.IOException;

import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.security.core.Authentication;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.sharemarking.application.common.dto.UserDetailImpl;
import com.sharemarking.application.common.service.ValidateCodeService;

@Controller
public class IndexController {
	
	@Value("${project.session.loginValidateCode}")
	private String loginKey;
	
	@Autowired
	private ValidateCodeService vcs;
	
	@RequestMapping(value={"","index"},method=RequestMethod.GET)
	public String index(Authentication authentication,Model model,HttpServletRequest request){
		UserDetailImpl udi=(UserDetailImpl)authentication.getPrincipal();
		model.addAttribute("name", udi.getName());
		return "index";
	}
	
	@RequestMapping(value="login",method=RequestMethod.GET)
	public void login(){
	}
	
	@RequestMapping(value="loginValidateCode",method=RequestMethod.GET)
	public void loginValidateCode(HttpServletResponse response,HttpSession session) throws IOException{
		Object[] validateCode=vcs.createImage();
		session.setAttribute(loginKey, validateCode[0]);
		response.setContentType("image/png");
		ImageIO.write((BufferedImage)validateCode[1], "png", response.getOutputStream());
	}
}
